package cn.lanchengkeji.datafillingsystem.utils;

import org.bouncycastle.util.encoders.Base64;

import java.io.IOException;
import java.io.InputStream;

public class CodeUtil {

	/**
	 * base64编码
	 *
	 * @param inputByte
	 *            字节数组
	 * @return 字节数组
	 */
	public static byte[] base64Encode(byte[] inputByte) {
		return Base64.encode(inputByte);
	}

	/**
	 * 字节数组转换为十六进制字符串
	 *
	 * @param bytearr
	 *            字节数组
	 * @return 十六进制字符串
	 */
	public static String byteArr2HexString(byte[] bytearr) {
		if (bytearr == null) {
			return "null";
		}
		StringBuffer sb = new StringBuffer();

		for (int k = 0; k < bytearr.length; k++) {
			if ((bytearr[k] & 0xFF) < 16) {
				sb.append("0");
			}
			sb.append(Integer.toString(bytearr[k] & 0xFF, 16));
		}
		return sb.toString();
	}

	/**
	 * 十六进制字符串转换为字节数组
	 *
	 * @param hexstring
	 *            十六进制字符串
	 * @return 字节数组
	 */
	public static byte[] hexString2ByteArr(String hexstring) {
		if ((hexstring == null) || (hexstring.length() % 2 != 0)) {
			return new byte[0];
		}

		byte[] dest = new byte[hexstring.length() / 2];

		for (int i = 0; i < dest.length; i++) {
			String val = hexstring.substring(2 * i, 2 * i + 2);
			dest[i] = (byte) Integer.parseInt(val, 16);
		}
		return dest;
	}

	/**
	 * 将字节流转化为 Base64 字符串
	 *
	 * @param in
	 * @return
	 */
	public static String inputStreamToBase64Str(InputStream in) {
		byte[] data = null;
		// 读取图片字节数组
		try {
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return new String(base64Encode(data));// 返回Base64编码过的字节数组字符串
	}

}